using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using VOL.Entity.DomainModels;
using System.Collections.Generic;

namespace VOL.demo.Controllers
{
    public partial class SalesReturnOrderModelController
    {
        /// <summary>
        /// 分页查询销售退货订单
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderCode">退货单号</param>
        /// <param name="status">单据状态</param>
        /// <param name="supplierId">供应商ID</param>
        /// <param name="returnDate">退货日期</param>
        /// <returns>分页结果</returns>
        [HttpGet("GetPageList")]
        public async Task<IActionResult> GetPageList(
            int pageIndex = 1,
            int pageSize = 10,
            string orderCode = null,
            int? status = null,
            long? supplierId = null,
            DateTime? returnDate = null)
        {
            try
            {
                var result = await Service.GetSalesReturnOrderPageListAsync(
                    pageIndex, pageSize, orderCode, status, supplierId, returnDate);

                if (result.Status)
                {
                    return Json(result);
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 根据销售单号查询销售明细列表（用于退货时选择明细）
        /// </summary>
        /// <param name="shopOrderCode">销售单号</param>
        /// <returns>销售明细列表</returns>
        [HttpGet("GetSalesOrderDetailsByCode")]
        public async Task<IActionResult> GetSalesOrderDetailsByCode(string shopOrderCode)
        {
            try
            {
                if (string.IsNullOrEmpty(shopOrderCode))
                {
                    return Json(new { success = false, message = "销售单号不能为空" });
                }

                var result = await Service.GetSalesOrderDetailsByCodeAsync(shopOrderCode);

                if (result.Status)
                {
                    return Json(result);
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = $"查询销售明细失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 保存销售退货订单（包括主表和明细表）
        /// </summary>
        /// <param name="request">退货订单数据（不需要传id和isDel字段，由系统自动生成）</param>
        /// <returns>保存结果</returns>
        /// <remarks>
        /// 请求示例：
        /// 
        ///     POST /api/SalesReturnOrderModel/SaveSalesReturnOrder
        ///     {
        ///       "mainData": {
        ///         "orderCode": "XSDH1256",
        ///         "salesOrderItemCode": "2",
        ///         "returnDate": "2025-10-29T11:25:27.390Z",
        ///         "supplierId": 2,
        ///         "contact": "2",
        ///         "contactPhone": "2",
        ///         "deptId": "3d8aac6-888d-4484-bd12-b8b02938006z",
        ///         "userId": 2,
        ///         "returnReason": "2",
        ///         "remark": "2",
        ///         "status": 2,
        ///         "auditor": "2",
        ///         "auditTime": "2025-10-29T11:25:27.390Z",
        ///         "customerName": "2"
        ///       },
        ///       "detailData": [
        ///         {
        ///           "purchaseOrderItemCode": "2",
        ///           "goodsId": 2,
        ///           "goodsCode": "2",
        ///           "goodsName": "2",
        ///           "specModel": "2",
        ///           "unitId": 2,
        ///           "unitName": "2",
        ///           "returnQuantity": 2,
        ///           "purchasePrice": 2,
        ///           "remarks": "2",
        ///           "stockOutItemId": 2
        ///         }
        ///       ]
        ///     }
        /// 
        /// </remarks>
        [HttpPost("SaveSalesReturnOrder")]
        public async Task<IActionResult> SaveSalesReturnOrder([FromBody] SalesReturnOrderSaveRequest request)
        {
            try
            {
                if (request == null)
                {
                    return Json(new { success = false, message = "请求数据不能为空" });
                }

                var result = await Service.SaveSalesReturnOrderAsync(request);

                if (result.Status)
                {
                    return Json(result);
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = $"保存失败：{ex.Message}" });
            }
        }
    }
}

